package com.whut.whw.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.whut.whw.R;
import com.whut.whw.base.BaseFragment;
import com.whut.whw.opersoncenter.OPersonCenterActivity;
import com.whut.whw.util.GsonUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *  desc:
 *  Created by 忘尘无憾 on 2017/06/15.
 *  version:
 * </pre>
 */
public class MsgMeFragment extends BaseFragment {

    private RecyclerView msg_me_recycler_view;//Recycler滚动控件
    private SwipeRefreshLayout msg_me_swipe_refresh;//下拉刷新控件
    private List<MsgInformModel> informList;//数据集
    private MsgRecyclerViewAdapter msgRecyclerViewAdapter;//适配器
    private LinearLayoutManager layoutManager;//布局管理器
    private boolean isLoading = false;//默认未加载

    public static MsgMeFragment newInstance() {
        return new MsgMeFragment();
    }

    @Override
    public View initView(LayoutInflater inflater) {
        View rootView = inflater.inflate(R.layout.fragment_msg_me, null);
        msg_me_recycler_view = (RecyclerView) rootView.findViewById(R.id.msg_me_recycler_view);
        msg_me_swipe_refresh = (SwipeRefreshLayout) rootView.findViewById(R.id.msg_me_swipe_refresh);
        msg_me_swipe_refresh.setColorSchemeResources(R.color.colorPrimary);//设置刷新条颜色
        return rootView;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        informList = new ArrayList<>();
        try {
            JSONArray array = new JSONArray();
            for (int i = 0; i < 3; i++) {
                JSONObject obj = new JSONObject();

                obj.put("msgImgUrl", "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg");
                obj.put("msgName", "唐晨" + i);
                obj.put("msgContentFlag", "评论");
                obj.put("msgTime", "2017-06-16");
                obj.put("msgContent", "“淘气”就是那闹事的猫。它在东皇城根穷人家里，原叫做‘小黑’。李太太嫌‘小 \n" +
                        "黑’的称谓太俗，又笑说：“那跟门房‘老白’不成了一对儿么？老白听了要生气的”");
                array.put(obj);
            }
            informList = GsonUtils.jsonToList(array.toString(), MsgInformModel.class);

        } catch (Exception e) {
            e.printStackTrace();
        }
        msgRecyclerViewAdapter = new MsgRecyclerViewAdapter(context, informList);
        //设置布局管理器
        layoutManager = new LinearLayoutManager(context);
        msg_me_recycler_view.setLayoutManager(layoutManager);
        msg_me_recycler_view.setAdapter(msgRecyclerViewAdapter);
    }

    @Override
    public void setListener() {

        //滚动到底部刷新
        msg_me_recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //获取最后一项position
                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == msgRecyclerViewAdapter.getItemCount()) {
                    boolean isRefreshing = msg_me_swipe_refresh.isRefreshing();
                    if (isRefreshing) {// SwipeRefreshLayout正在刷新
                        msgRecyclerViewAdapter.notifyItemRemoved(msgRecyclerViewAdapter.getItemCount());
                        return;
                    }
                    if (!isLoading) {
                        isLoading = true;
                        appendData();
                    }
                }
            }
        });

        //下拉刷新
        msg_me_swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshData();//更新数据
            }
        });

        //每项的点击事件
        msgRecyclerViewAdapter.setOnItemClickListener(new MsgRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {//点击
                getUiDelegate().toastLong(String.valueOf(position));
            }

            @Override
            public void onItemLongClick(View view, int position) {//长按
                getUiDelegate().toastLong("哈哈啊哈"+String.valueOf(position));
            }

            @Override
            public void onImgClick(View view, int position) {//点击头像
                getUiDelegate().toastLong("图片"+position);
                startActivity(new Intent(context, OPersonCenterActivity.class));
            }
        });
    }

    /**
     * 下拉刷新更新数据
     * tc 2017-6-16
     */
    private void refreshData() {
        try {
            JSONArray array = new JSONArray();
            for (int i = 0; i < 2; i++) {
                JSONObject obj = new JSONObject();

                obj.put("msgImgUrl", "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg");
                obj.put("msgName", "唐晨" + i);
                obj.put("msgContentFlag", "回复");
                obj.put("msgTime", "2017-06-16");
                obj.put("msgContent", "“淘气”就是那闹事的猫。它在东皇城根穷人家里，原叫做‘小黑’。李太太嫌‘小 黑’的称谓太俗，又笑说：“那跟门房‘老白’不成了一对儿么？老白听了要生气的”");
                obj.put("msgDynamic", "我是一个动态");
                array.put(obj);
            }
            informList.clear();
            informList.addAll(GsonUtils.jsonToList(array.toString(), MsgInformModel.class));
            msgRecyclerViewAdapter.notifyDataSetChanged();
            msg_me_swipe_refresh.setRefreshing(false);//关闭下拉刷新
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 上拉刷新更新数据
     * 当滑动到底部的时候进行刷新操作
     * tc 2017-6-16
     */
    private void appendData() {
        try {
            JSONArray array = new JSONArray();
            for (int i = 0; i < 2; i++) {
                JSONObject obj = new JSONObject();
                if(i%2==1){
                    obj.put("msgImgUrl", "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg");
                    obj.put("msgName", "唐晨" + i);
                    obj.put("msgTime", "2017-06-16");
                    obj.put("msgContent", "对您进行了点赞");
                    obj.put("msgDynamic", "我是一个动态");
                    obj.put("type", "1");
                }else{
                    obj.put("msgImgUrl", "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg");
                    obj.put("msgName", "唐晨" + i);
                    obj.put("msgContentFlag", "回复");
                    obj.put("msgTime", "2017-06-16");
                    obj.put("msgContent", "“淘气”就是那闹事的猫。它在东皇城根穷人家里，原叫做‘小黑’。李太太嫌‘小 黑’的称谓太俗，又笑说：“那跟门房‘老白’不成了一对儿么？老白听了要生气的”");
                    obj.put("msgDynamic", "我是一个动态");
                }
                array.put(obj);
            }
            informList.addAll(GsonUtils.jsonToList(array.toString(), MsgInformModel.class));
            isLoading = false;
            msgRecyclerViewAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
